package com.xgr.wonderful.utils;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

/**
 * 重点要区分 两个点，一个是正在定位中，二是已经定位写成。
 * 只要执行了 doLocate，就肯定已经开启了定位动作。
 * @author lsj
 *
 */
public class LocationUtils implements BDLocationListener {

	public static final String TAG = "LocationUtils";
	private static LocationUtils locationUtils = null;

	private Double mLatitude;
	private Double mLongtitude;
	private boolean isInistialed = false;
	private boolean isClientStoped = true;
	private boolean isLocated = false; //定位是否写完成
	private boolean isLocating = false ; //是否正在定位中

	private int errcode = 0;

	public static final int LOCATE_MAX_TRY_TIME = 6;
	// 已经尝试定位的次数
	// 当一次定位失败时会继续尝试进行定位，最多尝试6次
	private int requesttime = 0;

	private LocationClient mLocationClient;
	private LocationClientOption mLocationClientOption;

	private LocationUtils() {

	}

	public static LocationUtils getInstance() {
		if (locationUtils == null) {
			syncInit() ;
		}
		return locationUtils;
	}
	
	private static synchronized void syncInit (){
		if (locationUtils == null){
			locationUtils = new LocationUtils() ;
		}
	}

	public Double getLatitude() {
		return mLatitude;
	}

	public void setLatitude(Double mLatitude) {
		this.mLatitude = mLatitude;
	}

	public Double getLongtitude() {
		return mLongtitude;
	}

	public void setLongtitude(Double mLongtitude) {
		this.mLongtitude = mLongtitude;
	}

	public void setIsLocated(boolean isLocated) {
		this.isLocated = isLocated;
	}

	public boolean getIsLocated() {
		return isLocated == true;
	}
	public void setIsLocating(boolean isLocating ){
		this.isLocating = isLocating ;
	}
	public boolean getIsLocating (){
		return isLocating;
	}
	// 获得错误码
	public int getErrcode() {
		return errcode;
	}

	public int getRequesttime() {
		return requesttime;
	}

	
	// 初始化
	public void initLocation(Context context) {
		if (isInistialed)
			return;
		mLocationClient = new LocationClient(context);
		mLocationClientOption = new LocationClientOption();
		// 打开gps
		mLocationClientOption.setOpenGps(true);
		// 请求定位时间间隔不小于5s
		mLocationClientOption.setScanSpan(1000);
		mLocationClient.setLocOption(mLocationClientOption);
		mLocationClient.registerLocationListener(this);
		mLocationClient.start();
		isInistialed = true;
		isClientStoped = false;
		setIsLocated(false);
		setIsLocating(false) ;
	}

	/**
	 * 请求一次定位操作
	 * 如果返回false则表示法需要先进行initialize
	 * @return
	 */
	public boolean doLocate() {
		if (!isInistialed) {
			LogUtils.i(TAG, "please initial location before do locate");
			return false;
		}
		// 当发起一次定位请求后Baidu地图会不停的进行定位操作
		// 所以只好在onReceiveLocation中只好将mLocationClient关闭
		// 当需要再次进行定位操作时则先检查一下mLocationClient是已经开启
		if (isClientStoped ==true){
			mLocationClient.start();
			isClientStoped = false;
		}
		// 这里还需要更多的优化，可以根据手机当前所处的状态选择合适的定位请求策略
		mLocationClient.requestLocation();
		setIsLocated(false);
		setIsLocating(true) ;
		return true;
	}
	//关闭定位服务 
	public void closeLocation (){
		if (isClientStoped == false){
			isClientStoped = true;
			mLocationClient.stop();
		}
	}
	
	@Override
	public void onReceiveLocation(BDLocation location) {
		LogUtils.i(TAG, "onRecieve");
		requesttime ++;
		if (location == null){
			return;
		}
		int errcode = location.getLocType();
		// 还有更多错误情况有待考虑
		switch (errcode) {
		case 63:
			break;

		default:
			break;
		}
		if (errcode != 161 ){  //定位 不成功， 只有为161时成功
			if (requesttime>= LOCATE_MAX_TRY_TIME){
				requesttime =0;
				closeLocation();
				setIsLocated(false);//在dolocate后，只有定位失败会出现这种情况 
				setIsLocating(false) ;
			}
			return;
		}
		LogUtils.i(TAG, "errcode:"+errcode);
		// 定位请求成功
		// 设置经纬度
		setLatitude(location.getLatitude());
		setLongtitude(location.getLongitude());
		LogUtils.i(TAG, "location is done ");
		requesttime =0;
		setIsLocated(true);
		setIsLocating(false) ;
		closeLocation();

	}


}
